package com.spring.dao.impl;

import com.spring.dao.AccountDao;
import com.spring.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;

@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private QueryRunner queryRunner;



    @Override
    public void save(Account account) {
        //编写sql
        String sql = "insert into account values(null,?,?)";

        try {
            queryRunner.update(sql,account.getName(),account.getMoney());
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public Account findById(int id) {
        String sql = "select * from account where id = ?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(Account.class), id);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from account where id = ?";

        try {
            queryRunner.update(sql,id);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }
}
